package com.xiaoxiong.text;

import java.util.ArrayList;
import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import android.content.Context;

import com.xiaoxiong.bean.CourseData;
import com.xiaoxiong.database.OperateFormOptionalCourse;


public class DealElectiveCourseInfo {
	private Context context;
	private String html;
	private Document content;
	private String Year;
	private String[] data;
	private List<CourseData> electiveCourses = new ArrayList<CourseData>();

	public DealElectiveCourseInfo(Context context, String html) {
		super();
		this.context = context;
		this.html = html;
		this.content = Jsoup.parse(this.html);
		Year = new String();
		data = new String[9];
		for (int i = 0; i < 9; ++i) {
			data[i] = new String();
		}
	}

	public void StartDeal() {
		init();
		int end = 0;
		int f = 0;
		boolean flag = true;
		StringBuilder sbyear = new StringBuilder(Year);
		while (sbyear.length() > 0) {
			f = 0;
			for (int m = 0; m < 9;) {
				flag = true;
				data[m] = "";
				end = sbyear.indexOf("@");
				for (int j = 0; j < end; ++j) {
					char c = sbyear.charAt(j);
					if (c == '无' && m == 0) {
						f++;
						flag = false;
						break;
					} else {
						data[m] += c;
					}
				}
				if (flag || f > 1) {
					++m;
				}
				sbyear.delete(0, end + 1);
			}
			if (f <= 1) {
				CourseData requireData = new CourseData("",
						data[0], data[1], data[2], data[3], data[4], data[5],
						data[6], (data[6] + "------" + data[7]), data[8]);
				electiveCourses.add(requireData);
			} else {
				CourseData requireData = electiveCourses
						.get(electiveCourses.size() - 1);
				electiveCourses.remove(electiveCourses.size() - 1);

				String score = requireData.getScore();

				requireData.setScore(score + "\n" + data[6] + "------"
						+ data[7]);

				electiveCourses.add(requireData);
			}
		}
		saveData();
	}

	private void saveData() {
		OperateFormOptionalCourse ofo = new OperateFormOptionalCourse(context);
		ofo.deleteAllOptionalCourse();
		int size = electiveCourses.size();
		for (int i = 0; i < size; ++i) {
			CourseData requireData = electiveCourses.get(i);
			ofo.insertOptionalCourse(requireData.getSemester(),
					requireData.getCourseCode(), requireData.getCourseName(),
					requireData.getCredit(), requireData.getExamMethod(),
					requireData.getFirstCourse(),
					requireData.getRepairCourse(), requireData.getStudyTime(),
					requireData.getScore(), requireData.getHadCredit());
		}
	}

	private void init() {
		Elements element = content.getElementsByTag("td");
		StringBuilder value = new StringBuilder();
		for (Element links : element) {
			String title = links.text();
			value.append(title + "\n");
		}

		value.delete(0, value.indexOf("已选修"));
		value.delete(value.indexOf("选修课已修学分"), value.length());
		value = new StringBuilder(value.toString().replaceAll("\n", "@"));
		value.delete(0, value.indexOf("@") + 1);
		value.append(" ");
		Year = deal(value);
	}

	private String deal(StringBuilder sbValue) {
		StringBuilder sb = new StringBuilder();
		for (int j = 0; j < sbValue.length(); ++j) {
			char c = sbValue.charAt(j);
			sb.append(c);
			if (c == '@' && sbValue.charAt(j + 1) == '@') {
				sb.append("无");
			}
		}
		return sb.toString().replaceAll(" ", "");
	}
}
